
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./CSS/reset.css">
    <style>
        table{
            width: 400px;
            margin: 100px auto;
            /* height: 200px; */
            font-size: 14px;
        }
        thead tr{
            width: 100%;
        }
        thead{
            background-color: #ccc;
            width: 100%;
        }
        thead th {
            height: 25px;
            vertical-align: middle;
            font-weight: bold;
        }
        table tbody tr{
            border-bottom: solid 1px #ccc ;
            /* display: inline-block; */
            height: 30px;
        }

        table tbody td{
            vertical-align: middle;
            text-align: center;
            color:rgb(56, 110, 211);
        }
    </style>
</head>
<body>
   
    <table class="clearfix">
        <thead>
            <tr>
                <th>姓名</th>
                <th>年龄</th>
                <th>成绩</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody class="data-tbody">
        
        </tbody>
    </table>
    <script>
        //先准备数据
        var datas = [
            {
                name:'张三',
                subject:'Java',
                score:99
            },
            {
                name:'张小三',
                subject:'Java',
                score:81
            },
            {
                name:'李四',
                subject:'Java',
                score:75
            },
            {
                name:'李小四',
                subject:'Java',
                score:67
            }
        ];
        
        var tbody = document.querySelector('tbody');
        //所有的数据都生成在tbody当中，行数就是数组的长度
        for(var i = 0;i < datas.length;i++){
            //创建行
            var tr = document.createElement('tr');
            tbody.appendChild(tr);
            // 行里创建和数据有关的单元格，单元格的个数取决于属性的个数
            for(var k in datas[i]){
                //创建单元格
                var td = document.createElement('td');
                //把对象的属性值给td
                td.innerHTML = datas[i][k];
                tr.appendChild(td);
            }
            //创建操作的单元格
            var td = document.createElement('td');
            td.innerHTML = '<a href="javascript:;">删除</a>';
            tr.appendChild(td);
        }

        //删除操作开始
        //获取所有a
        var as = document.querySelectorAll('a');
        for(var i = 0;i < as.length;i++){
            as[i].onclick = function(){
                tbody.removeChild(this.parentNode.parentNode);
            }
        }
        
    </script>
</body>
</html>